package com.sinothk.map.amap;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.amap.api.maps.model.LatLng;

import java.util.ArrayList;
import java.util.List;

public class MapTool {

    /**
     * 通过经纬度获取距离(单位：米)
     *
     * @param centerLat
     * @param centerLon
     * @param lat
     * @param lng
     * @return
     */
    public static double getDistance(double centerLat, double centerLon, double lat, double lng) {
        // ==================================
        double EARTH_RADIUS = 6378.137;

        // ==================================
        double radLat1 = rad(centerLat);
        double radLat2 = rad(lat);
        double a = radLat1 - radLat2;
        double b = rad(centerLon) - rad(lng);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s * 1000;
        return s;
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * BD-09 坐标转换成 GCJ-02 坐标
     */
    public static LatLng BD2GCJ(LatLng bd) {
        double x = bd.longitude - 0.0065, y = bd.latitude - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI);

        double lng = z * Math.cos(theta);//lng
        double lat = z * Math.sin(theta);//lat
        return new LatLng(lat, lng);
    }

    /**
     * GCJ-02 坐标转换成 BD-09 坐标
     */
    public static LatLng GCJ2BD(LatLng bd) {
        double x = bd.longitude, y = bd.latitude;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * Math.PI);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * Math.PI);
        double tempLon = z * Math.cos(theta) + 0.0065;
        double tempLat = z * Math.sin(theta) + 0.006;
        return new LatLng(tempLat, tempLon);

    }

    /**
     * 检测程序是否安装
     *
     * @param packageName
     * @return
     */
    public static boolean hasInstalled(Context mContext, String packageName) {

        PackageManager manager = mContext.getPackageManager();

        //获取所有已安装程序的包信息
        List<PackageInfo> installedPackages = manager.getInstalledPackages(0);
        if (installedPackages != null) {
            for (PackageInfo info : installedPackages) {
                if (info.packageName.equals(packageName))
                    return true;
            }
        }
        return false;
    }

    public static ArrayList<String> hasInstalled(Context context, String[] packageNameArr) {

        final PackageManager packageManager = context.getPackageManager();//获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程bai序的du包信息
        List<String> pName = new ArrayList<String>();//用于存储所有已安装程序的包名
        //从pinfo中将包名字逐一取出，压入pName list中
        for (int i = 0; i < pinfo.size(); i++) {
            String pn = pinfo.get(i).packageName;
            pName.add(pn);
        }

        ArrayList<String> hasAppPnList = new ArrayList<>();

        for (String markPN : packageNameArr) {
            if (pName.contains(markPN)) {
                hasAppPnList.add(markPN);
            }
        }
        return hasAppPnList;//判断pName中是否有目标程序的包名，有TRUE，没有FALSE
    }
}
